#include "temperature_show.h"

temperature_Show::temperature_Show(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    timer = new QTimer(this);
    timer->start(1000);
    ui_init();
ui_Btn_Init();
connect_init();
}
void temperature_Show::connect_init()
{

    //刷新
    connect(timer,&QTimer::timeout,[=](){
        update_data();
    });
    //确定
    connect(theBtn[0],&QPushButton::clicked,[=](){

        });
    connect(theBtn[1],&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
        });
}

void temperature_Show::ui_init()
{
    for(int i=0;i<11;i++)//创建界面显示的label
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 20px;\
                                color: #000000;			                 /* 文本颜色 */\
                           }"
          );
    }

    theLabel[0]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
    theLabel[1]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
    theLabel[2]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

   theLabel[0]->setGeometry(0,0,640,60);//设定每隔放置一个按键
   theLabel[1]->setGeometry(0,0,300,60);//设定每隔放置一个按键
   theLabel[2]->setGeometry(340,0,300,60);//设定每隔放置一个按键
   theLabel[0]->setText("版本信息");

   QGridLayout *mainLayout = new QGridLayout();


   mainLayout->addWidget(theLabel[3], 0, 1);
   mainLayout->addWidget(theLabel[4], 0, 2);
   mainLayout->addWidget(theLabel[5], 1, 1);
   mainLayout->addWidget(theLabel[6], 1, 2);
   mainLayout->addWidget(theLabel[7], 2, 1);
   mainLayout->addWidget(theLabel[8], 2, 2);
   mainLayout->addWidget(theLabel[9], 3, 1);
   mainLayout->addWidget(theLabel[10], 3, 2);


   theLabel[3]->setText("目标零气流量");
   theLabel[5]->setText("实际零气流量");
   theLabel[7]->setText("目标气体流量");
   theLabel[9]->setText("实际气体流量");
   update_data();

   mainLayout->setContentsMargins(150,60,150,150);//(int left, int top, int right, int bottom)
   mainLayout->setSizeConstraint(QLayout::SetMinimumSize);//布局显示效果
   QWidget* widget = new QWidget(this);
   widget->setGeometry(0,0,640,480);
   widget->setLayout(mainLayout);


   }

    void temperature_Show::ui_Btn_Init()
    {
        for(int i=0;i<2;i++)//创建主屏幕显示的按键
        {

             theBtn[i] = new QPushButton(this);
    //        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
            theBtn[i]->geometry();
    }

        theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/.png); /* 背景图片 */"+"}"
         "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/2.png); /* 背景图片 */"+"}");
        theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "border-radius:4px;  border: 1px groove white; padding:1px 4px; border-style: outset;"+ "background-image: url(:/pic/回.png); /* 背景图片 */"+"}"
         "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/返2.png); /* 背景图片 */"+"}");

    theBtn[0]->setGeometry(50,130,90,50 );
    theBtn[0]->setText(tr("确定"));
    theBtn[0]->installEventFilter(this);
    theBtn[1]->setGeometry(240,360,160,50 );
    theBtn[1]->setText(tr("返回"));
    theBtn[1]->installEventFilter(this);
   theBtn[0]->hide();
    }

    void temperature_Show::update_data()
    {

        theLabel[4]->setText(QString::number(Sysstatus->Int_Temp1,'f',1));
        theLabel[6]->setText(QString::number(Sysstatus->Ozo_TempRead,'f',1));
        theLabel[8]->setText(QString::number(Sysstatus->Phot_TempRead,'f',1));
        theLabel[10]->setText(QString::number(Sysstatus->Int_Temp3,'f',1));

    }


    bool temperature_Show::eventFilter(QObject *watched, QEvent *event)
    {
        if(theBtn[0] == watched||theBtn[1] == watched)
    {
        if(event->type() == QEvent::KeyPress)
        {
            QKeyEvent *ev = (QKeyEvent *)event;
            QPushButton *button = (QPushButton *)watched;

           {
               switch (ev->key())
               {
               case LEFT://左
               {
                      focusPreviousChild();
                 break;
               }
               case RIGHT://右
               {
                      focusNextChild();
              break;
               }
               case OK:
               {
                   emit  button-> clicked();
                 break;
               }
               case BACK:
               {
                   this->parentWidget()->show();
                   this->close();
                   break;
               }
               case INIT:
               {
                   init_Show *show = new init_Show(this);
                   show->show();
                   this->hide();

                   break;
               }
                default:
                   break;
               }
           }
        }
        }
        return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

    }
